package com.focusai.efairy.ui.activity.dev;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.focusai.efairy.R;
import com.focusai.efairy.model.project.CompanyCodeItem;
import com.focusai.efairy.model.project.UsageCodeItem;
import com.focusai.efairy.model.project.VersionItem;
import com.focusai.efairy.model.request.GetDeviceCompanyCodeListRequest;
import com.focusai.efairy.model.request.GetDeviceUsageCodeListRequest;
import com.focusai.efairy.model.request.GetDeviceVersionListRequest;
import com.focusai.efairy.model.response.DeviceCompanycodeListResponse;
import com.focusai.efairy.model.response.DeviceUsagecodeListResponse;
import com.focusai.efairy.model.response.DeviceVersionListResponse;
import com.focusai.efairy.network.HttpManager;
import com.focusai.efairy.network.Response;
import com.focusai.efairy.network.exception.NetworkException;
import com.focusai.efairy.ui.adapter.base.BaseRecyclerAdapter;
import com.focusai.efairy.ui.adapter.base.BaseRecyclerViewHolder;
import com.focusai.efairy.ui.adapter.base.RecycleViewDivider;
import com.focusai.efairy.ui.adapter.warn.UpgradeAdapter;
import com.focusai.efairy.ui.base.SwipeBackBaseActivity;
import com.focusai.efairy.utils.ScreenUtil;
import com.mylhyl.crlayout.SwipeRefreshAdapterView;
import com.mylhyl.crlayout.SwipeRefreshRecyclerView;

import java.util.ArrayList;
import java.util.List;

public class DevCompanyCodeActivity extends SwipeBackBaseActivity{

//    private RecyclerView mRecylerView;
    private UpgradeAdapter mAdapter;
    private SwipeRefreshRecyclerView swipeRefreshRecyclerView;
    private View iv_no_data;
    List<Object> list;
    private String lastId="0";
    private String enterprise_id,purpose_id;
    private String activity_type;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity__company_code_list);
        Intent intent =  getIntent();
        if(intent!=null){
            if(intent.hasExtra("activity_type")){
                activity_type = intent.getStringExtra("activity_type");
            }
            enterprise_id = intent.getStringExtra("enterprise_id");
            purpose_id = intent.getStringExtra("purpose_id");
        }


        initView();
        setDefaultValues();

    }


    @Override
    protected void initView() {
        swipeRefreshRecyclerView  = findView(R.id.swipe_recyce_view);
//        mRecylerView = findView(R.id.msg_rcyc_view);
        iv_no_data =  findView(R.id.iv_no_data);
    }

    @Override
    protected void setDefaultValues() {
        list =  new ArrayList<>();
        swipeRefreshRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        mAdapter = new UpgradeAdapter(getContext());
        swipeRefreshRecyclerView.getScrollView().addItemDecoration(new RecycleViewDivider(
                mContext, LinearLayoutManager.VERTICAL, ScreenUtil.dp2pix(1), getResources().getColor(R.color.main_line_gray)));
        swipeRefreshRecyclerView.setAdapter(mAdapter);
        mAdapter.refreshData(list);
//        swipeRefreshRecyclerView.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
//            @Override
//            public void onRefresh() {
////                if (list.size() > 0) {
////                    swipeRefreshRecyclerView.setRefreshing(false);
////                    return;
////                }
//                loadData();
//            }
//
//        });

        swipeRefreshRecyclerView.setOnListLoadListener(new SwipeRefreshAdapterView.OnListLoadListener() {
            @Override
            public void onListLoad() {
                loadData();
            }
        });
        mAdapter.setOnItemClickListener(new BaseRecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseRecyclerViewHolder holder) {
                if (holder == null) return;
                int position = swipeRefreshRecyclerView.getScrollView().getChildLayoutPosition(holder.itemView);
                Intent intent = new Intent();
                if("companycode".equals(activity_type)){
                    CompanyCodeItem item = (CompanyCodeItem)mAdapter.getData().get(position);
                    intent.putExtra("code_id",item.getEfairydevicecompanycode_id()+"");
                    intent.putExtra("code_name",item.getEfairydevicecompanycode_name());
                    setResult(DevUpgradeActivity.RESULT_OK,intent);
                }else if("usagecod".equals(activity_type)){
                    UsageCodeItem item = (UsageCodeItem)mAdapter.getData().get(position);
                    intent.putExtra("code_id",item.getEfairydeviceusagecode_id()+"");
                    intent.putExtra("code_name",item.getEfairydeviceusagecode_name());
                    setResult(DevUpgradeActivity.RESULT_OK,intent);
                }else if("version".equals(activity_type)){
                    VersionItem item = (VersionItem)mAdapter.getData().get(position);
                    intent.putExtra("code_id",item.getEfairydeviceversion_id()+"");
                    intent.putExtra("code_name",item.getEfairydeviceversion_version());
                    setResult(DevUpgradeActivity.RESULT_OK,intent);
                }

                finish();

            }
        });
//        swipeRefreshRecyclerView.setRefreshing(true);
        swipeRefreshRecyclerView.setEnabledLoad(true);
//        swipeRefreshRecyclerView.setLoading(true);
        swipeRefreshRecyclerView.setEnabled(false);
        loadData();


    }

    @Override
    public void initTitte() {
        setCenterTitleName(R.string.dev_enterprise_info_list);
    }


    //获取信息
    @Override
    protected void loadData() {
        if("companycode".equals(activity_type)){
            getCompanyCodeList();
        }else if("usagecod".equals(activity_type)){
            getPurpose();
        }else if("version".equals(activity_type)){
            geVersion();
        }

    }


    private void getCompanyCodeList(){
        HttpManager.getInstance().sendRequest(new GetDeviceCompanyCodeListRequest(lastId,new Response.Listener<DeviceCompanycodeListResponse>() {

            @Override
            protected void onSuccess(DeviceCompanycodeListResponse response) {
                if(response !=null){
                    list.addAll(response.getCompanycode_list());
                    if(list!= null && list.size() > 0){
                        lastId = ((CompanyCodeItem)list.get(list.size()-1)).getEfairydevicecompanycode_id()+"";
                    }
                    mAdapter.notifyDataSetChanged();
                    if(response.getTotal_rows() == list.size()){
                        swipeRefreshRecyclerView.setEnabledLoad(false);
                    }else{
                        swipeRefreshRecyclerView.setEnabledLoad(true);
                    }
                    swipeRefreshRecyclerView.setLoading(false);
                }

                showTip();
            }

            @Override
            protected void onFail(NetworkException exception) {
                swipeRefreshRecyclerView.setLoading(false);
                showToast("获取数据失败");
                doException(exception);
                showTip();
            }
        }));
    }



    //获取用途
    private void getPurpose(){
        HttpManager.getInstance().sendRequest(new GetDeviceUsageCodeListRequest(lastId,new Response.Listener<DeviceUsagecodeListResponse>() {

            @Override
            protected void onSuccess(DeviceUsagecodeListResponse response) {
                if(response !=null){
                    list.addAll(response.getUsagecode_list());
                    if(list!= null && list.size() > 0){
                        lastId = ((UsageCodeItem)list.get(list.size()-1)).getEfairydeviceusagecode_id()+"";
                    }

                    mAdapter.notifyDataSetChanged();
                    if(response.getTotal_rows() == list.size()){
                        swipeRefreshRecyclerView.setEnabledLoad(false);
                    }else{
                        swipeRefreshRecyclerView.setEnabledLoad(true);
                    }
                    swipeRefreshRecyclerView.setLoading(false);
                }

                showTip();
            }

            @Override
            protected void onFail(NetworkException exception) {
                swipeRefreshRecyclerView.setLoading(false);
                showToast("获取数据失败");
                doException(exception);
                showTip();
            }
        }));

    }



    //获取版本
    private void geVersion(){
        HttpManager.getInstance().sendRequest(new GetDeviceVersionListRequest(enterprise_id, purpose_id, lastId,new Response.Listener<DeviceVersionListResponse>() {

            @Override
            protected void onSuccess(DeviceVersionListResponse response) {
                if(response !=null){
                    list.addAll(response.getVersion_list());
                    if(list!= null && list.size() > 0){
                        lastId = ((VersionItem)list.get(list.size()-1)).getEfairydeviceversion_id()+"";
                    }

                    mAdapter.notifyDataSetChanged();
                    if(response.getTotal_rows() == list.size()){
                        swipeRefreshRecyclerView.setEnabledLoad(false);
                    }else{
                        swipeRefreshRecyclerView.setEnabledLoad(true);
                    }
                    swipeRefreshRecyclerView.setLoading(false);
                }

                showTip();
            }

            @Override
            protected void onFail(NetworkException exception) {
                swipeRefreshRecyclerView.setLoading(false);
                showToast("获取数据失败");
                doException(exception);
                showTip();
            }
        }));

    }


    private void showTip() {
        if (mAdapter.getData() != null && mAdapter.getData().size() > 0) {
            iv_no_data.setVisibility(View.GONE);
        } else {
            iv_no_data.setVisibility(View.VISIBLE);
        }
    }

}
